<?php
class tj extends CI_Controller {
	function index() {
		$this -> pub -> logp('浏览统计', '2');
		$da = $this -> pub -> get_setting('统计', 'Statistics');

		$da['title'] = '统计';
		$da['index'] = 25;

		//统计
		$this -> db -> select('count(*)');
		$q = $this -> db -> get_where('member');
		$ls = $q -> row_array(0);
		$da['member'] = $ls['count(*)'];

		$this -> db -> select('count(*)');
		$this -> db -> where('wz_del', 1);
		$q = $this -> db -> get_where('wz');
		$ls = $q -> row_array(0);
		$da['wz'] = $ls['count(*)'];

		$this -> db -> select('count(*)');
		$this -> db -> where('sp_del', 1);
		$q = $this -> db -> get_where('sp');
		$ls = $q -> row_array(0);
		$da['sp'] = $ls['count(*)'];

		//$this -> db -> select('count(*)');
		//$q = $this -> db -> get_where('log');
		//$ls = $q -> row_array(0);
		$da['log'] = 0;

		$this -> db -> select('count(*)');
		$q = $this -> db -> get_where('bill');
		$ls = $q -> row_array(0);
		$da['bill'] = $ls['count(*)'];
		$this -> load -> view('adminx/tj', $da);
	}

	function tj_memberx() {
		$this -> pub -> logp('统计会员', '2');
		$day1 = strtotime($this -> input -> post('day1'));
		$day2 = strtotime($this -> input -> post('day2')) + 86400;
		$num = ($day2 - $day1) / 86400;
		if ($day2 > $day1) {
			$da = '';
			for ($i = 0; $i < $num; $i++) {
				$da['memberx']['categories'][] = date('m-d', $day1 + $i * 86400);
			}
			$type = array('普通客户', '准微店店长', '微店店长', '禁言');
			foreach ($type as $k => $v) {
				$da['memberx']['series'][$k]['name'] = $v;
				for ($i = 0; $i < $num; $i++) {
					$lsx = $day1 + $i * 86400;
					$this -> db -> select('count(*)');
					$this -> db -> where('subscribe_time >', $lsx);
					$this -> db -> where('subscribe_time <', ($lsx + 86400));
					$this -> db -> where('type', ($k + 1));
					$q = $this -> db -> get_where('member');
					$ls = $q -> row_array(0);
					$da['memberx']['series'][$k]['data'][] = intval($ls['count(*)']);
				}
			}
		}
		$this -> db -> select('count(*)');
		$q = $this -> db -> get_where('member');
		$lsx = $q -> row_array(0);

		$this -> db -> select('count(*)');
		$this -> db -> where('sex', 1);
		$q = $this -> db -> get_where('member');
		$ls = $q -> row_array(0);
		$da['sex']['data'][] = array('男', intval($ls['count(*)'] / $lsx['count(*)'] * 100));

		$this -> db -> select('count(*)');
		$this -> db -> where('sex', 2);
		$q = $this -> db -> get_where('member');
		$ls = $q -> row_array(0);
		$da['sex']['data'][] = array('女', intval($ls['count(*)'] / $lsx['count(*)'] * 100));

		echo json_encode($da);
	}

	function tj_log() {
		$this -> pub -> logp('统计日志', '2');
		$day1 = strtotime($this -> input -> post('day1'));
		$day2 = strtotime($this -> input -> post('day2')) + 86400;
		$num = ($day2 - $day1) / 86400;
		if ($day2 > $day1) {
			$da = '';
			for ($i = 0; $i < $num; $i++) {
				$da['logx']['categories'][] = date('m-d', $day1 + $i * 86400);
			}
			$da['logx']['series'][0]['name'] = '日志';
			for ($i = 0; $i < $num; $i++) {
				$lsx = $day1 + $i * 86400;
				$this -> db -> select('count(*)');
				$this -> db -> where('log_mktime >', $lsx);
				$this -> db -> where('log_mktime <', ($lsx + 86400));
				$q = $this -> db -> get_where('log');
				$ls = $q -> row_array(0);
				$da['logx']['series'][0]['data'][] = intval($ls['count(*)']);
			}
		}
		echo json_encode($da);
	}

	function tj_bill() {
		$this -> pub -> logp('统计表单', '2');
		$day1 = strtotime($this -> input -> post('day1'));
		$day2 = strtotime($this -> input -> post('day2')) + 86400;
		$num = ($day2 - $day1) / 86400;
		if ($day2 > $day1) {
			$da = '';
			for ($i = 0; $i < $num; $i++) {
				$da['bill']['categories'][] = date('m-d', $day1 + $i * 86400);
			}
			$da['bill']['series'][0]['name'] = '订单';
			for ($i = 0; $i < $num; $i++) {
				$lsx = $day1 + $i * 86400;
				$this -> db -> select('count(*)');
				$this -> db -> where('mktime >', $lsx);
				$this -> db -> where('mktime <', ($lsx + 86400));
				$q = $this -> db -> get_where('bill');
				$ls = $q -> row_array(0);
				$da['bill']['series'][0]['data'][] = intval($ls['count(*)']);
			}
		}
		echo json_encode($da);
	}

	function tj_jb() {
		$this -> pub -> logp('统计现金流', '2');
		$day1 = strtotime($this -> input -> post('day1'));
		$day2 = strtotime($this -> input -> post('day2')) + 86400;
		$num = ($day2 - $day1) / 86400;
		if ($day2 > $day1) {
			$da = '';
			for ($i = 0; $i < $num; $i++) {
				$da['bill']['categories'][] = date('m-d', $day1 + $i * 86400);
			}
			$da['bill']['series'][0]['name'] = $this -> input -> post('name');
			for ($i = 0; $i < $num; $i++) {
				$lsx = $day1 + $i * 86400;
				$this -> db -> where('type', $this -> input -> post('type'));
				$this -> db -> select('sum(je)');
				$this -> db -> where('mktime >', $lsx);
				$this -> db -> where('mktime <', ($lsx + 86400));
				$q = $this -> db -> get_where('jb');
				$ls = $q -> row_array(0);
				$da['bill']['series'][0]['data'][] = intval($ls['sum(je)']);
			}
		}
		echo json_encode($da);
	}

}
?>